﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.Classes
{
    public class DeleteOrderHelper
    {
        public static bool DeleteOrder(string OrderId)
        {
            MSQDBEntities dc = new MSQDBEntities();
            ORDER order = dc.ORDERs.Find(OrderId);
            if (order == null)
                return false;
            // Get all Bag
            List<BAG> bags = new List<BAG>();
            foreach (var bag in order.ORDER_PRODUCT)
            {
                bags.AddRange(bag.BAGs);
            }
            // Delete all Bag Information
            while (bags.Count > 0)
            {
                BAG bag = bags[0];
                // Bag_Process_History
                while (bag.BAG_PROCESS_HISTORY.Count > 0)
                {
                    dc.BAG_PROCESS_HISTORY.Remove(bag.BAG_PROCESS_HISTORY.First());
                }

                // Bag_Process
                while (bag.BAG_PROCESS.Count > 0)
                {
                    dc.BAG_PROCESS.Remove(bag.BAG_PROCESS.First());
                }

                // Bag_Mould
                while (bag.BAG_MOULD.Count > 0)
                {
                    dc.BAG_MOULD.Remove(bag.BAG_MOULD.First());
                }

                // Bag_Stone
                while (bag.BAG_STONE.Count > 0)
                {
                    dc.BAG_STONE.Remove(bag.BAG_STONE.First());
                }

                // Bag_Component
                while (bag.BAG_COMPONENT.Count > 0)
                {
                    dc.BAG_COMPONENT.Remove(bag.BAG_COMPONENT.First());
                }

                // Stone_Export_Bag
                while (bag.STONE_EXPORTBAG.Count > 0)
                {
                    STONE_EXPORTBAG stone_export_bag = bag.STONE_EXPORTBAG.First();
                    while (stone_export_bag.STONE_EXPORTBAGDETAIL.Count > 0)
                    {
                        dc.STONE_EXPORTBAGDETAIL.Remove(stone_export_bag.STONE_EXPORTBAGDETAIL.First());
                    }
                    bag.STONE_EXPORTBAG.Remove(bag.STONE_EXPORTBAG.First());
                    dc.STONE_EXPORTBAG.Remove(stone_export_bag);
                }

                // Component_Export_Bag
                while (bag.COMPONENT_EXPORTBAG.Count > 0)
                {
                    COMPONENT_EXPORTBAG component_export_bag = bag.COMPONENT_EXPORTBAG.First();
                    while (component_export_bag.COMPONENT_EXPORTBAGDETAIL.Count > 0)
                    {
                        dc.COMPONENT_EXPORTBAGDETAIL.Remove(component_export_bag.COMPONENT_EXPORTBAGDETAIL.First());
                    }
                    bag.COMPONENT_EXPORTBAG.Remove(bag.COMPONENT_EXPORTBAG.First());
                    dc.COMPONENT_EXPORTBAG.Remove(component_export_bag);
                }
                // Delete Bag
                dc.BAGs.Remove(bag);
                bags.RemoveAt(0);
            }

            // Order_Product
            while (order.ORDER_PRODUCT.Count > 0)
            {
                dc.ORDER_PRODUCT.Remove(order.ORDER_PRODUCT.First());
            }

            // Order
            dc.ORDERs.Remove(order);
            try
            {
                dc.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}